package com.example.hw2.controller;


import com.example.hw2.dao.StudentDao;
import com.example.hw2.entity.Course;
import com.example.hw2.entity.Student;
import com.example.hw2.util.DaoUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

@Controller
@RequestMapping("/Student")

public class DesignController {
    private Student s;
    private List<Course> checkList;


    @GetMapping("/addStudent")
    public String addStudentInfo(Model model){
        //跳转到学生个人信息页面
        Student s=new Student();
        model.addAttribute("Student",s);

        return "Student";
    }

    @GetMapping("/Course")
    public String selectCourse(@Valid @ModelAttribute("Student") Student student, Errors errors, Model model){
        SqlSession sqlSession= DaoUtil.getSqlSession();
        StudentDao studentdao=sqlSession.getMapper(StudentDao.class);


        if(errors.hasErrors()){
           return "Student";
        }


        //实例化学生对象
        s=new Student();
        s=student;


        int flag=studentdao.addStudent(new Student(student.getName(),student.getId(),student.getGrade(),student.getProfession(),student.getCollege(),null));
        sqlSession.commit();
        sqlSession.close();

        //添加课程
        List<Course> courses= Arrays.asList(
                new Course("高等数学"),
                new Course("高等代数"),
                new Course("离散数学"),
                new Course("数据库"),
                new Course("软件工程"),
                new Course("计算机组成原理"),
                new Course("JavaWeb"),
                new Course("Python"),
                new Course("C++"),
                new Course("篮球"),
                new Course("足球"),
                new Course("乒乓球"),
                new Course("两性心理"),
                new Course( "手工艺"),
                new Course("LOL打野科目二")
        );


        model.addAttribute("Student",s);
        model.addAttribute("courses",courses);

        return "Course";
    }

    @PostMapping("/submitCourse")
    public String showResult(Model model,HttpServletRequest request){
        checkList=new ArrayList<>();
        String[] checkCourses=request.getParameterValues("checkCourses");


        for(int i=0;i<checkCourses.length;i++)
            checkList.add(new Course(checkCourses[i]));

        s.setCourseList(checkList);


        //获取连接池对象
        SqlSession sqlSession= DaoUtil.getSqlSession();
        //映射
        StudentDao studentdao=sqlSession.getMapper(StudentDao.class);

        Map<String, Object> map = new HashMap<>();

        //将学生id存入map
        map.put("sid",s.getId());


        for (int i=0;i<checkList.size();i++) {
            //将课程信息存入map
            map.put("cname", checkList.get(i).getName());
            studentdao.addCourse(map);
            sqlSession.commit();


        }
        sqlSession.close();


        model.addAttribute("Student",s);
        model.addAttribute("courses",checkList);
        return "Result";
    }
}
